<template>
  <div class="app-container">
    <div class="head-container">
      <Search/>
      <crudOperation  :permission="permission">
        <el-button
          slot="left"
          class="filter-item"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :loading="crud.delAllLoading"
          @click="confirmDelAll()"
        >
          清空
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-refresh-right"
          size="mini"
          @click="open('restartWebFront')"
        >
          重启WEB前端
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-refresh-right"
          size="mini"
          @click="open('restartWebBackend')"
        >
          重启WEB后端
        </el-button>
<!--        <el-button-->
<!--          slot="left"-->
<!--          class="filter-item"-->
<!--          type="primary"-->
<!--          icon="el-icon-suitcase"-->
<!--          size="mini"-->
<!--          @click="open('packageJar')"-->
<!--        >-->
<!--          打包WEB后端-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          slot="left"-->
<!--          class="filter-item"-->
<!--          type="success"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="open('downloadJar')"-->
<!--        >-->
<!--          下载WEB后端JAR包-->
<!--        </el-button>-->
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-refresh-right"
          size="mini"
          @click="open('restartAppletBackend')"
        >
          重启小程序后端
        </el-button>
<!--        <el-button-->
<!--          slot="left"-->
<!--          class="filter-item"-->
<!--          type="primary"-->
<!--          icon="el-icon-suitcase"-->
<!--          size="mini"-->
<!--          @click="open('packageAppletJar')"-->
<!--        >-->
<!--          打包小程序后端-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          slot="left"-->
<!--          class="filter-item"-->
<!--          type="success"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="open('downloadAppletJar')"-->
<!--        >-->
<!--          下载小程序后端JAR包-->
<!--        </el-button>-->
        <el-button
          slot="left"
          class="filter-item"
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="open('flushCache')"
        >
          清空缓存
        </el-button>
      </crudOperation>
    </div>
    <!--表格渲染-->
    <el-table class="limit-height" ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;"
              @selection-change="crud.selectionChangeHandler">
      <el-table-column :render-header="crud.renderHeader" type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="请求方法">
              <span>{{ props.row.method }}</span>
            </el-form-item>
            <el-form-item label="请求参数">
              <span>{{ props.row.params }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('username')" prop="username" label="用户名"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('requestIp')" prop="requestIp" label="IP"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('address')" :show-overflow-tooltip="true" prop="address" label="IP来源"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('description')" prop="description" label="描述"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('browser')" prop="browser" label="浏览器"/>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('time')" prop="time" label="请求耗时" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.time <= 300">{{ scope.row.time }}ms</el-tag>
          <el-tag v-else-if="scope.row.time <= 1000" type="warning">{{ scope.row.time }}ms</el-tag>
          <el-tag v-else type="danger">{{ scope.row.time }}ms</el-tag>
        </template>
      </el-table-column>
      <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('createTime')" prop="createTime" label="创建日期" width="180px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination/>
    <el-dialog
      :close-on-click-modal="false"
      :visible="isShow"
      :before-close="close"
      title="选择环境"
      width="400px"
      top="20%"
    >
      <el-form size="small" ref="form" label-width="60px" :rules="rules">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="环境:" prop="env">
              <el-select v-model="env" style="width: 100%">
<!--                <el-option label="开发" value="dev"></el-option>-->
                <el-option label="测试" value="test"></el-option>
                <el-option label="正式" value="prod"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="公司:" prop="companyCode">
              <template>
                <el-select v-model="companyCode" style="width: 100%">
                  <div v-if="user.userId == 1">
                    <div v-if="webServer0.includes(user.companyCode)">
                      <el-option v-for="item of webServer0" :value="item"/>
                    </div>
                    <div v-else-if="webServer1.includes(user.companyCode)">
                      <el-option v-for="item of webServer1" :value="item"/>
                    </div>
                    <div v-else-if="webServer2.includes(user.companyCode)">
                      <el-option v-for="item of webServer2" :value="item"/>
                    </div>
                    <div v-else-if="webServer3.includes(user.companyCode)">
                      <el-option v-for="item of webServer3" :value="item"/>
                    </div>
                    <div v-else-if="webServer4.includes(user.companyCode)">
                      <el-option v-for="item of webServer4" :value="item"/>
                    </div>
                    <div v-else-if="webServer5.includes(user.companyCode)">
                      <el-option v-for="item of webServer5" :value="item"/>
                    </div>
                  </div>
                </el-select>
              </template>

<!--              <el-select v-model="companyCode" style="width: 100%">-->
<!--                <el-option label="000000" value="000000"></el-option>-->
<!--&lt;!&ndash;                <el-option label="100000[测试]" value="100000"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="100001[测试]" value="100001"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="001[天台]" value="001"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="002[温岭]" value="002"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="003[临海]" value="003"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="004[温岭太平]" value="004"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="005[椒江]" value="005"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="006[杭州]" value="006"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="007[路桥]" value="007"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="008[乐清]" value="008"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="009[慈溪]" value="009"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="010[余姚]" value="010"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="011[仙居]" value="011"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="998[临隆食品]" value="998"></el-option>&ndash;&gt;-->
<!--&lt;!&ndash;                <el-option label="999[王建酒业]" value="999"></el-option>&ndash;&gt;-->
<!--                <el-option label="999999[虎门]" value="999999"></el-option>-->
<!--              </el-select>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="close">取消</el-button>
        <el-button :loading="restart" type="primary" @click="restartWebFront">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Search from './search'
import crudLogAPi, {delAllInfo} from '@/api/monitor/sysLog'
import CRUD, {presenter} from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import {mapGetters} from 'vuex'

// crud交由presenter持有
const defaultCrud = CRUD({title: '日志', url: 'api/sysLog'})
export default {
  name: 'Log',
  components: {Search, crudOperation, pagination},
  mixins: [presenter(defaultCrud)],
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  data() {
    return {
      webServer0:['000000'],//101.34.21.216 测试
      webServer1:['001','002','003','004','005','006','007','008','009','010','012','013','014'],//121.41.75.32 百威
      webServer2:['000','016','017','018','019','020','021','107','108','109','110'],//124.223.13.184 百威+非百威
      webServer3:['011','015','333','998','999','101','102','103','104','105','106'],//47.122.38.187 非百威
      webServer4:['999999'],//120.79.174.93 虎门
      webServer5:['ty'],//47.110.134.7 童盈
      permission: {
        add: ["admin", "log:list"],
        edit: ["admin", "log:list"],
        del: ["admin", "log:list"],
        download: ["admin", "log:list"],
        import: ["admin", "log:list"],
        export: ["admin", "log:list"],
        print: ["admin", "log:list"],
      },
      isShow: false,
      restart: false,
      env: null,
      companyCode: null,
      type: null,
      rules: {
        // env: [
        //   {required: true, message: '环境不能为空', trigger: 'blur'}
        // ],
      },
    }
  },
  created() {
    this.crud.optShow = {
      add: false,
      edit: false,
      del: false,
      download: false
    }
  },
  methods: {
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    confirmDelAll() {
      this.$confirm(`确认清空所有操作日志吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        delAllInfo().then(res => {
          this.crud.delAllLoading = false
          this.crud.dleChangePage(1)
          this.crud.delSuccessNotify()
          this.crud.toQuery()
        }).catch(err => {
          this.crud.delAllLoading = false
          console.log(err.response.data.message)
        })
      }).catch(() => {
      })
    },
    close() {
      this.isShow = false
      this.type = null
    },
    restartWebFront() {
      // if (!this.env) {
      //   this.$message.error('请选择环境')
      // } else {
        // this.crud.loadingOpen()
        crudLogAPi[this.type](this.env,this.companyCode).then(res => {
          this.isShow = false
          // this.crud.loadingClose()
          this.$notify({title: '操作成功', type: 'success'})
          if (res.length > 0) {
            console.log(123)
            const a = document.createElement('a')
            a.setAttribute('href', res)
            a.click()
          }
        }).catch(() => {
          // this.crud.loadingClose()
        })
      // }
    },
    open(type) {
      this.isShow = true
      this.type = type
    }
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 70px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 100%;
}

.demo-table-expand .el-form-item__content {
  font-size: 12px;
}
</style>
